﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static AccountOperations.AccountOperations;

namespace Bank_database_system.业务部.贷款业务
{
    public partial class 贷款办理a : UserControl
    {
        private string branch_id = "";
        private string staff_id = "";
        public event EventHandler ButtonBack;
        贷款办理b loanProcessing = new 贷款办理b();
        public 贷款办理a()
        {
            InitializeComponent();
            loanProcessing.ButtonBack += BackToA;
        }

        public void branch_and_staff_get(string branchID, string staffID)
        {
            branch_id = branchID;
            staff_id = staffID;
        }

        // 贷款办理详细界面的返回
        private void BackToA(object sender, EventArgs e)
        {
            textBox5.Text = "";
            // 修改控件状态      
            panel1.Visible = false;
            panel1.Controls.Clear();
        }

        // 贷款办理的取消
        private void button1_Click(object sender, EventArgs e)
        {
            textBox5.Text = "";
            ButtonBack?.Invoke(this, EventArgs.Empty);
        }

        // 卡号输入时只能输入数字的限制
        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBox5.TextLength == 20)
            {
                if (e.KeyChar != '\b')
                    e.Handled = true;
            }
            else
            {
                if ((e.KeyChar > '9' || e.KeyChar < '0') && e.KeyChar != '\b')
                    e.Handled = true;
            }
        }

        private bool CardIDCheck()
        {
            if (textBox5.Text == "")
            {
                MessageBox.Show("卡号不能为空！");
                textBox5.Text = "";
                return false;
            }
            DataTable dataTable = BankCardInquiry(textBox5.Text);
            if (!CheckBankCard(textBox5.Text) || dataTable == null || dataTable.Rows.Count == 0)
            {
                MessageBox.Show("卡号不存在，请检查卡号后再试！");
                textBox5.Text = "";
                return false;
            }
            return true;
        }

        // 贷款办理的确认
        private void button9_Click(object sender, EventArgs e)
        {
            //卡号验证
            if (!CardIDCheck())
            {
                return;
            }
            using (输入密码 passwords = new 输入密码(textBox5.Text))
            {
                // 显示 输入密码 作为模态窗体
                DialogResult p = passwords.ShowDialog();
                if (p == DialogResult.OK)
                {
                    // 获取 输入密码 中的结果
                    //切换到下一页面
                    panel1.Visible = true;
                    panel1.Controls.Clear();
                    loanProcessing.information_Set(textBox5.Text);
                    loanProcessing.branch_and_staff_get(branch_id, staff_id);
                    panel1.Controls.Add(loanProcessing);
                    panel1.BringToFront();//显示置顶
                }
                else if (p == DialogResult.No)
                {
                    MessageBox.Show("卡号或密码错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (p == DialogResult.Yes)
                {
                    MessageBox.Show("该账户已被冻结", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
